package com.wffz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wffz.mapper.MoneyLogMapper;
import com.wffz.po.ClientUser;
import com.wffz.po.MoneyLog;
import com.wffz.service.MoneyLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author sunlei
 * @className MoneyLogServiceImpl
 * @Description
 * @date 2023-02-05 11:13
 **/
@Service
@Slf4j
public class MoneyLogServiceImpl implements MoneyLogService {


    @Resource
    private MoneyLogMapper moneyLogMapper;

    @Override
    public boolean saveMoneyLog(MoneyLog moneyLog) {
        return moneyLogMapper.insert(moneyLog) > 0;
    }

    @Override
    public Float sumMoney(ClientUser clientUser) {
        float result = 0;
        QueryWrapper<MoneyLog> queryWrapper = new QueryWrapper<MoneyLog>();
        queryWrapper.select("sum(money) as money");
        queryWrapper.eq("user_id", clientUser.getId());
        MoneyLog ord = moneyLogMapper.selectOne(queryWrapper);
        //注意，空指针问题
        if (ord == null) {
            result = Float.valueOf(0);
        } else {
            result = ord.getMoney();
        }
        return result;
    }

    @Override
    public int moneyCount(ClientUser clientUser) {
        QueryWrapper<MoneyLog> queryWrapper = new QueryWrapper<MoneyLog>();
        queryWrapper.eq("user_id", clientUser.getId());
        return moneyLogMapper.selectCount(queryWrapper);
    }
}
